package com.witfortos.common.utils

import com.witfortos.common.base.IBaseResponse
import com.witfortos.common.http.ResponseThrowable
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
/**
 *   @auther : tan
 *   time   : 2020-07-10
 */
@ExperimentalCoroutinesApi
fun <T> Flow<IBaseResponse<T>>.applyTransform(): Flow<T> {
    return this
            .flowOn(Dispatchers.IO)
            .map {
                if (it.isSuccess()) return@map it.data()
                else throw ResponseThrowable(it.responeCode(), it.responeMsg())
            }
}